package com.kaiwukj.android.ufamily.mvp.ui.page.order.refund;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class OrderRefundAdapter extends BaseQuickAdapter<OrderResult, BaseViewHolder> {

    public static final int DELETE = 1;
    public static final int CANCEL = 2;

    private OnEvent mEvent;

    public OrderRefundAdapter() {
        super(R.layout.item_order_refund, new ArrayList<>());
        //   addChildClickViewIds(R.id.btn_order_delete, R.id.btn_order_detail);
        addChildClickViewIds(R.id.btn_order_detail);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    public void setOnEvent(OnEvent event) {
        this.mEvent = event;
    }

    public void removeById(int id) {
        for (int i = 0; i < getData().size(); i++) {
            if (id == getData().get(i).getId()) {
                removeAt(i);
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, OrderResult orderResult) {
        holder.setText(R.id.tv_time, orderResult.getCreateTime());
        holder.setText(R.id.tv_status, orderResult.getRefundStatusStr());
        holder.setText(R.id.tv_service_name, orderResult.getServiceName());
        holder.setText(R.id.tv_service_rule, String.format("规格: %s", orderResult.getServiceParameterName()));
        holder.setText(R.id.tv_order_refund, String.format("退款: ¥%s", orderResult.getActualPriceStr()));
        Button btnDelete = holder.getView(R.id.btn_order_delete);
        ViewGroup containerRefund = holder.getView(R.id.container_refund);
        Glide.with(getContext())
                .load(orderResult.getThumbnail())
                .placeholder(R.drawable.picture_image_placeholder)
                .into((ImageView) holder.getView(R.id.iv_image));
        if (orderResult.getRefundStatus() == 1) {
            containerRefund.setVisibility(View.GONE);
            btnDelete.setText("取消退款");
        } else {
            btnDelete.setText("删除记录");
            containerRefund.setVisibility(View.VISIBLE);
            TextView tvRefundFlag = holder.getView(R.id.tv_refund_status);
            holder.setText(R.id.tv_refund_desc, orderResult.getRefundFailReason());
            if (orderResult.getRefundStatus() == 2) {
                tvRefundFlag.setText("退款成功");
                tvRefundFlag.setTextColor(ContextCompat.getColor(getContext(), R.color.text_01));
            } else {
                tvRefundFlag.setText("退款失败");
                tvRefundFlag.setTextColor(ContextCompat.getColor(getContext(), R.color.app_color_theme));
            }
        }

        btnDelete.setOnClickListener(v -> {
            if (mEvent != null) {
                if (orderResult.getRefundStatus() == 1) {//退款审核中
                    mEvent.onOrderEvent(holder.getAdapterPosition(), orderResult.getId(), CANCEL);
                } else {
                    mEvent.onOrderEvent(holder.getAdapterPosition(), orderResult.getId(), DELETE);
                }
            }
        });

    }

    public interface OnEvent {
        void onOrderEvent(int eventPos, long resultId, int event);
    }
}
